package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Project;
import com.manger.model.ZEHQ;
import com.manger.utils.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ZEHQFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025-10-14 17:22
 * @Version 1.0
 */
public class XEHQFilesFormat implements CaseFilesFormat {

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        System.out.println("++++++++++++++++++++++小鹅还贷案件数据整理+++++++++++++++++++++++++++++");
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        System.out.println("读取委托信息表：" + excelFilePath);
        Map<String,List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), ZEHQ.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<ZEHQ> zehqList = (List<ZEHQ>) map.get(map.keySet().iterator().next());
        if(zehqList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        Map<String,String> tempMap = new HashMap<String,String>();
        for(ZEHQ zEHQ : zehqList){
           try{
               String resourcePath = extractDir + "/" + project.getDebtName() + "/" + zEHQ.getCncCardNo() + "_" + zEHQ.getUserName();
               String tempTargetDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/"
                       + zEHQ.getUserName() + "-" + zEHQ.getIdCard();
               File resourceFileDir = new File(resourcePath);
               if(!resourceFileDir.exists()){
                   continue;
               }
               File tempTargetFileDir = new File(tempTargetDir);
               if(!tempTargetFileDir.exists()){
                   tempTargetFileDir.mkdirs();
               }
               CopyFiles.copyBaseFiles(tempTargetDir ,project);
               CopyFiles.copyDirToDir(resourcePath,tempTargetDir + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
               System.out.println("整理案件成功：【"+zEHQ.getUserName()+"-"+zEHQ.getIdCard()+"】");
               tempMap.put(zEHQ.getIdCard(),tempTargetDir);
           }catch (Exception e){
               System.out.println("整理案件失败：【"+zEHQ.getUserName()+"-"+zEHQ.getIdCard()+"】");
           }
        }
        System.out.println("整理目标材料");
        String mFilePath = extractDir + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/";
        File mFile = new File(mFilePath);
        if(mFile.exists()){
            File[] files = mFile.listFiles();
            for(File file : files){
                String mFileType = file.getName();
                File[] mFiles = file.listFiles();
                File tempFile = null;
                for(File mTempFile : mFiles){
                    String fileName = mTempFile.getName();
                    boolean flag = false;
                    String tempTargetDir = "";
                    for(String key: tempMap.keySet()){
                        if(fileName.contains(key)){
                            flag = true;
                            tempFile = mTempFile;
                            tempTargetDir = tempMap.get(key);
                            break;
                        }
                    }
                    if(!flag){
                        continue;
                    }
                    tempTargetDir += "/" + mFileType + "/";
                    CopyFiles.copyFileToDir(tempFile.getCanonicalPath(),tempTargetDir);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        ChinaAreaTools.init();
        ProjectUtils.init();
        XEHQFilesFormat xehqFilesFormat = new XEHQFilesFormat();
        String target = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        String extractDir = "C:\\Users\\liuhj\\Desktop\\小鹅还钱";
        xehqFilesFormat.format(extractDir,target,ProjectUtils.projectMap.get(DetNameEnum.XEHQ));
    }
}
